package Offer;

/*
* 复杂链表的复制
*       1.使用map,key:旧结点,value:新结点
*       2.拼接加拆分
* */
//拼接加拆分
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class Offer35copyRandomList {
    public Node copyRandomList(Node head) {
        if(head==null){
            return head;
        }
        //拼接next
        Node curr=head;
        while(curr!=null){
            Node tmp=new Node(curr.val);
            tmp.next=curr.next;
            curr.next=tmp;
            curr=tmp.next;
        }
        //拼接random
        curr=head;
        while(curr!=null){
            if(curr.random!=null){
                curr.next.random=curr.random.next;
            }
            curr=curr.next.next;
        }
        //拆分
        curr=head.next;
        Node pre=head,res=head.next;
        while(curr.next!=null){
            curr.next=curr.next.next;
            pre.next=pre.next.next;
            pre=pre.next;
            curr=curr.next;
        }
        pre.next=null;
        return res;
    }
}
